<?php
/**
 * Climbia Plugin
 *
 * @author Teino Anasazi
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
 */

elgg_register_event_handler('init', 'system', 'climbia_init');

/**
 * Climbia plugin initialization
*/
function climbia_init() {
	//CRUD de Escuelas, sectores e vias
	elgg_register_action("climbia/saveEscuela", elgg_get_plugins_path() . "climbia/actions/climbia/Escuela/saveEscuela.php");
	elgg_register_action("climbia/saveSector", elgg_get_plugins_path() . "climbia/actions/climbia/Sector/saveSector.php");
	elgg_register_action("climbia/saveVia", elgg_get_plugins_path() . "climbia/actions/climbia/Via/saveVia.php");
	elgg_register_action("climbia/deleteSector", elgg_get_plugins_path() . "climbia/actions/climbia/Sector/deleteSector.php");
	elgg_register_action("climbia/deleteEscuela", elgg_get_plugins_path() . "climbia/actions/climbia/Escuela/deleteEscuela.php");
	elgg_register_action("climbia/deleteVia", elgg_get_plugins_path() . "climbia/actions/climbia/Via/deleteVia.php");
	//Accions levada a cabo no perfil do usuario
	elgg_register_action("climbia/perfil/addVia", elgg_get_plugins_path() . "climbia/actions/climbia/Perfil/addVia.php");
	
	
	register_entity_type('object','escuela');
	register_entity_type('object','sector');
	//Aqui registro un hook para que me maneje as url de iconos das funcions que elgg ten para esto
	//o segundo parametro esta a object porque ten que ser un type, nn e valido un subtype entonces polo tanto teño que 
	//cachear todos os iconos de objetos. a linea de arriba intenta corregir 
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'climbia_icon_url_override');
	
	elgg_register_page_handler('climbia', 'climbia_page_handler');
	
	elgg_register_page_handler('climbiaicon', 'climbia_icon_handler');
	//Añade menus pero a todas as paginas sin importar o set_context();
	$sidebar = new ElggMenuItem('indexClimbia', elgg_echo('Escuelas'), 'climbia');
    elgg_register_menu_item('site', $sidebar);
	$sidebar = new ElggMenuItem('addEscuela', elgg_echo('Añadir Escuela'), 'climbia/addEscuela');
    elgg_register_menu_item('page', $sidebar);
    $sidebar = new ElggMenuItem('addSector', elgg_echo('Añadir Sector'), 'climbia/addSector');
    elgg_register_menu_item('page', $sidebar);
    $sidebar = new ElggMenuItem('addVia', elgg_echo('Añadir Via'), 'climbia/addVia');
    elgg_register_menu_item('page', $sidebar);
    $sidebar = new ElggMenuItem('listaescuelas', elgg_echo('Listar escuelas'), 'climbia/listaescuelas');
    elgg_register_menu_item('page', $sidebar);

}
    
	function climbia_page_handler($segments) {
		
		switch ($segments[0]) {
			case 'addEscuela':
				include elgg_get_plugins_path() . 'climbia/pages/addEscuela.php';
				return true;
				break;
				
			case 'addSector':
				include elgg_get_plugins_path() . 'climbia/pages/addSector.php';
				return true;
				break;
			case 'addVia':
				include elgg_get_plugins_path() . 'climbia/pages/addVia.php';
				return true;
				break;
			case 'listaescuelas':
				include elgg_get_plugins_path() . 'climbia/pages/listas/escuelas.php';
				return true;
				break;
			case 'verSector':
				include elgg_get_plugins_path() . 'climbia/pages/sector/ver.php';
				return true;
				break;
			case '':
				include elgg_get_plugins_path() . 'climbia/pages/listas/escuelas.php';
				return true;
				break;
			default:
				include elgg_get_plugins_path() . 'climbia/index.php';
				return true;
				break;
			
		} 
           
		return false;
	}
	
	/**
	 * Hook y handler para manejar iconos.
	 *
	 */
	
	function climbia_icon_handler($page) {
	
		// The username should be the file we're getting
		if (isset($page[0])) {
			set_input('climbia_guid', $page[0]);
		}
		if (isset($page[1])) {
			set_input('size', $page[1]);
		}
		// Include the standard profile index
		$plugin_dir = elgg_get_plugins_path();
		include("$plugin_dir/climbia/icon.php");
		return true;
	}
	
	/**
	 * Override the default entity icon for groups
	 *
	 * @return string Relative URL
	*/
	
	function climbia_icon_url_override($hook, $type, $returnvalue, $params) {
		$climbia = $params['entity'];
		$size = $params['size'];
		$subtipo = $climbia->getSubtype();
		
		if(($subtipo != 'escuela') && ($subtipo != 'sector') && ($subtipo != 'via')){
			return $returnvalue;
		}
	
		if (isset($climbia->icontime)) {
			// return thumbnail
			$icontime = $climbia->icontime;
			return "climbiaicon/$climbia->guid/$size/$icontime.jpg";
		}
	
		return "mod/groups/graphics/default{$size}.gif";
	}
 
?>
